package com.example.demo.nuoqi.controller;/*
 *author limfff
 *date 2021/4/26
 */

import com.example.demo.nuoqi.dao.UserMapper;
import com.example.demo.nuoqi.pojo.User;
import com.example.demo.nuoqi.service.UserService;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping
    public Result login(@RequestBody User user , HttpSession session) {
        User one = userMapper.selectOne(user);
        if (null != one && (one.getName().equals(user.getName()) && one.getPassword().equals(user.getPassword()))) {
            session.setAttribute("user", user);
            return new Result(true, StatusCode.OK, "登录成功", user);
        } else {
            return new Result(false, StatusCode.ERROR, "登录失败,此用户不存在", null);
        }
    }

    @GetMapping("/getUser")
    public Result getUser(HttpSession session) {

        User user = (User)session.getAttribute("user");
        if (null != user) {
            return new Result(true, StatusCode.OK, "获取用户成功", user);
        }else{
            return new Result(true, StatusCode.ERROR, "获取用户失败",null);
        }
    }



}
